package igetutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by zhihao.guo on 30/03/2018.
 * 早期得到抓包写的一次性代码
 */
public class IGetUtil {
    static String filePath = "/Users/plato1233/Desktop/得到抓包/谷歌方法论";

    public static void main(String[] args) throws IOException {
        find(filePath, 1);
        //    1.读取目录
        //    2.循环读取文件
        //    3.解析python字符串
        //    4.转码html
        //    5.解析mp3文件，并下载 下载完成，改名
        //    6.生成文件 作者_日期__内容_专栏.html 作者_日期__内容_专栏.mp3
        //    7.

    }

    static String buildaudio(String mp3) {
        return  "  <audio src=\""+mp3+"\" controls=\"controls\">\n" +
                "                Your browser does not support the audio element.\n" +
                "                </audio>";

    }
    public static void find(String pathName, int depth) throws IOException {
        int filecount = 0;
        //获取pathName的File对象
        File dirFile = new File(pathName);
        //判断该文件或目录是否存在，不存在时在控制台输出提醒
        if (!dirFile.exists()) {
            System.out.println("do not exit");
            return;
        }
        //判断如果不是一个目录，就判断是不是一个文件，时文件则输出文件路径
        if (!dirFile.isDirectory()) {
            if (dirFile.isFile()) {
                System.out.println(dirFile.getCanonicalFile());
            }
            return;
        }
        //获取此目录下的所有文件名与目录名
        String[] fileList = dirFile.list();
        int currentDepth = depth + 1;
        for (int i = 0; i < fileList.length; i++) {
            //遍历文件目录
            String string = fileList[i];
            //File("documentName","fileName")是File的另一个构造器
            File file = new File(dirFile.getPath(), string);

            System.out.println("name="+file.getName());
            if (!file.getName()
                     .contains("content")) {
                continue;
            }

            List<String> lines = Files.readLines(file, Charsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            for (String line : lines) {
                sb.append(line);
            }
            JSONObject jsonObject = JSON.parseObject(sb.toString());
            String title = (String) ((Map) jsonObject.get("c")).get("title");
            String html = (String) ((Map) ((Map) jsonObject.get("c")).get("article_content")).get("html");
            if (html.contains("\"mp3/")) {
                html = html.replace("\"mp3/", "\"http://igetoss.cdn.igetget.com/mp3/");
                int i1 = html.indexOf("data-key=\"");
                int i2 = html.indexOf(".mp3\"");
                int start = i1 + 10;
                String mp3file = html.substring(start, i2)+".mp3";
                System.out.println("mp3file = " + mp3file);

                int j1= html.indexOf("<div class=\"audio\">");
                int j2= html.indexOf("</h5> </div> </div>");

                String html_1 = html.substring(0, j1);
                String html_2 = html.substring(j2+"</h5> </div> </div>".length());
                html = html_1 + buildaudio(mp3file) + html_2;
            }


            //html = unicode2String(html).replace("\\\"","\"");
            String author_column = (String) ((Map) ((Map) jsonObject.get("c")).get("column_detail")).get("title");

            String date = (String) ((Map) jsonObject.get("c")).get("publish_time");
            date = StringUtils.substring(date, 0, 10);
            System.out.println("date = " + date);
            System.out.println("author_column = " + author_column);
            System.out.println("title = " + title);
            System.out.println(" ======================= ");

            String htmlfileNamePath = filePath + "/" + author_column + "_" + date + "_" + title + ".html";
            File filehtml = new File(htmlfileNamePath);
            Files.write(html.getBytes(), filehtml);
            String mp3fileName = author_column + "_" + date + "_" + title + ".mp3";
        }
    }


    //unicode转成string
    public static String unicode2String(String unicode) {
        if (StringUtils.isBlank(unicode)) return null;
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;

        while ((i = unicode.indexOf("\\u", pos)) != -1) {
            sb.append(unicode.substring(pos, i));
            if (i + 5 < unicode.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(unicode.substring(i + 2, i + 6), 16));
            }
        }

        return sb.toString();
    }

}
